package org.tysite.tyservice.example.jsonview.dto.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.tysite.tyservice.example.jsonview.dto.JsonViewAttributeDTO;

import java.io.IOException;
import java.util.List;

/**
 * List<JsonViewAttributeDto> 的JsonView序列化类
 * @author tysite
 * @date 2020/4/1 0001
 */
public class JsonViewAttributeListSerializer extends JsonSerializer<List<JsonViewAttributeDTO>> {
    
    @Override
    public void serialize(List<JsonViewAttributeDTO> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (null != value && value.size() > 0) {
            gen.writeStartArray();
            for (JsonViewAttributeDTO attributeDTO : value) {
                gen.writeStartObject();
                gen.writeNumberField("id", attributeDTO.getId());
                gen.writeStringField("name", attributeDTO.getName());
                gen.writeEndObject();
            }
            gen.writeEndArray();
        }
    }
    
}
